﻿using System;
using System.Collections.Generic;
using System.Linq;

using System.Data;
using Prodma.DataAccess.Database;
using Prodma.DataAccess.Interfaces.Models;
using Prodma.DataAccess.Models.ViewModels;
using Prodma.DataAccess.Repositories;
using Prodma.DataAccess.Helper.Enumerators;

namespace Prodma.DataAccess.DataService
{
    public class SectionService : ISectionRepository
    {

        private DataContext context = new DataContext();


        #region IRecordOperation<tb_Section,SectionVm> Members

        public IList<SectionVm> GetList()
        {
            DataContext context = new DataContext();
            using (context)
            {
                var list = (from section in context.tb_Section
                            select new SectionVm()
                            {
                                Id = section.Id,
                                Name = section.Name,
                                Description = section.Description,
                                Area = new AreaVm() { Id = section.tb_Area.Id, Name = section.tb_Area.Name },
                                State = new StateVm() { Id = section.tb_State.Id, Name = section.tb_State.Name }
                            }).OrderBy(x => x.Name).ToList();

                return list;
            }
        }
        public void New(tb_Section entity)
        {
            DataContext context = new DataContext();
            using (context)
            {
                context.AddTotb_Section(entity);
                context.SaveChanges();
            }
        }
        public SectionVm Edit(int id)
        {
            DataContext context = new DataContext();
            using (context)
            {
                var edit = (from section in context.tb_Section
                            where section.Id == id
                            select new SectionVm()
                            {
                                Id = section.Id,
                                Name = section.Name,
                                Description = section.Description,
                                Area = new AreaVm() { Id = section.tb_Area.Id, Name = section.tb_Area.Name },
                                State = new StateVm() { Id = section.tb_State.Id, Name = section.tb_State.Name }
                            }).FirstOrDefault();

                return edit;
            }
        }
        public void Update(int id, tb_Section entity)
        {
            DataContext context = new DataContext();
            using (context)
            {
                var edit = (from section in context.tb_Section
                            where section.Id == id
                            select section).FirstOrDefault();

                edit.Name = entity.Name;
                edit.Description = entity.Description;
                edit.tb_AreaReference.EntityKey = entity.tb_AreaReference.EntityKey;
                edit.tb_StateReference.EntityKey = entity.tb_StateReference.EntityKey;

                context.SaveChanges();
            }
        }
        public void Delete(int id)
        {
            DataContext context = new DataContext();
            using (context)
            {
                var delete = (from section in context.tb_Section
                              where section.Id == id
                              select section).FirstOrDefault();

                context.DeleteObject(delete);
                context.SaveChanges();
            }
        }
        #endregion

        #region IFindOperation<SectionVm> Members

        public IList<SectionVm> GetList(Dictionary<string, string> find)
        {
            string findName = find["name"];
            DataContext context = new DataContext();
            using (context)
            {
                var list = (from section in context.tb_Section
                            where section.Name.StartsWith(findName)
                            select new SectionVm()
                            {
                                Id = section.Id,
                                Name = section.Name,
                                Description = section.Description,
                                Area = new AreaVm() { Id = section.tb_Area.Id, Name = section.tb_Area.Name },
                                State = new StateVm() { Id = section.tb_State.Id, Name = section.tb_State.Name }
                            }).OrderBy(x => x.Name).ToList();

                return list;
            }
        }
        #endregion

        #region IStateOperation Members

        public void SetActive(int id)
        {
            DataContext context = new DataContext();
            using (context)
            {
                var viewState = (from section in context.tb_Section
                                 where section.Id == id
                                 select section).FirstOrDefault();

                viewState.tb_StateReference.EntityKey = new EntityKey("DataContext.tb_State", "Id", Convert.ToInt32(State.Active));

                context.SaveChanges();
            }
        }
        public void SetPassive(int id)
        {
            DataContext context = new DataContext();
            using (context)
            {
                var viewState = (from section in context.tb_Section
                                 where section.Id == id
                                 select section).FirstOrDefault();

                viewState.tb_StateReference.EntityKey = new EntityKey("DataContext.tb_State", "Id", Convert.ToInt32(State.Passive));

                context.SaveChanges();
            }
        }
        #endregion

    }
}
